package com.rchat.pocmini.activity.changenickname;


import com.aso.common.http.bean.BaseBean;
import com.aso.common.http.delegate.HttpCallbackImpl;
import com.rchat.pocmini.R;
import com.rchat.pocmini.account.PocAccount;
import com.rchat.pocmini.activity.fragment.groups.GroupsPresenterImpl;
import com.rchat.pocmini.bean2eventbus.EventBusAccountBean;
import com.rchat.pocmini.global.HttpCodeGlobal;
import com.rchat.pocmini.global.UrlsGlobal;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by ljd on 2017/8/31.
 */

public class ChangeNicknamePresenterImpl extends ChangeNicknameContract.Presenter {


    @Override
    void changeNickname(String userName, final String newNickName) {
        if ("".equals(newNickName)) {
            mView.showShortToast(R.string.nickname_empty_error);
            return;
        }
        if (newNickName.length() > 10) {
            mView.showShortToast(R.string.nickname_more_than_ten_error);
            return;
        }

        mModel.changeNickname(userName, newNickName, new HttpCallbackImpl<BaseBean>() {
            @Override
            public void onStart() {
                mView.showPb(true);
            }

            @Override
            public void onSuccess(BaseBean baseBean) {
                if (mView == null) {
                    return;
                }

                mView.showShortToast(baseBean.getDesc());
                PocAccount.setIsUserNameChanged(true);
                PocAccount.setNewUserName(newNickName);
                EventBusAccountBean eventBusAccountBean = new EventBusAccountBean();
                eventBusAccountBean.setCurrentEvent(EventBusAccountBean.EVENT_CHANGE_NICKNAME);
                eventBusAccountBean.setTarget(this.getClass(), GroupsPresenterImpl.class,"onEventBusRequestAccount");
                EventBus.getDefault().post(eventBusAccountBean);
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(bean.getDesc());
            }

            @Override
            public void onFinish() {
                if (null == mView) {
                    return;
                }
                mView.showPb(false);
            }
        });

    }
}
